package com.yztstudy.Controller;

import com.yztstudy.Entity.Cart;
import com.yztstudy.Entity.Desk;
import com.yztstudy.Entity.Food;
import com.yztstudy.Entity.FoodType;
import com.yztstudy.Service.CartService;
import com.yztstudy.Service.DeskService;
import com.yztstudy.Service.FoodService;
import com.yztstudy.Service.FoodTypeService;
import com.yztstudy.Utils.CartsTotalPriceUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Cart的表示层
 */

@Controller
@Scope("prototype") //spring注解的bean可以变成多例
public class CartController {

    @Autowired
    private CartService cartService;

    @Autowired
    private FoodService foodService;

    @Autowired
    private DeskService deskService;

    @Autowired
    private FoodTypeService foodTypeService;

    /**
     * 添加菜品到购物车
     */
    @GetMapping("/addCart/{foodID}/{deskID}/{typeID}")
    public void addCart(@PathVariable("foodID") Integer foodID, @PathVariable("deskID") Integer deskID, @PathVariable("typeID") Integer typeID, HttpServletResponse response) throws IOException
    {
        response.setContentType("text/html;charset=utf-8");

        //1.获取餐桌和菜品
        Desk desk = deskService.getDeskByID(deskID);
        Food food = foodService.getFoodById(foodID);

        //2.判断购物车中是否有菜品数据
        Map<String, Object> map = new HashMap<>();
        map.put("desk_id", deskID);
        map.put("food_id", foodID);
        int count = cartService.isExist(map);
        int result = 0;

        if (count == 0)
        {
            //不存在
            Cart cart = new Cart();
            cart.setNum(1);
            cart.setDesk(desk);
            cart.setFood(food);
            cart.setTotal(cart.getNum() * food.getPrice());
            result = cartService.addCart(cart); //新增购物车
        }else {
            //存在
            Cart cart = cartService.getMyCart(map);
            cart.setNum(cart.getNum() + 1);
            cart.setTotal(cart.getNum() * food.getPrice());
            result = cartService.updateCart(cart); //更新购物车
        }

        //3.响应给前端
        if (result > 0)
        {
            response.getWriter().write("<script>alert('添加到购物车成功!'); location.href = '/menu_UI/" + deskID + "/" + typeID + "/1';</script>");
        }else {
            response.getWriter().write("<script>alert('添加到购物车失败!'); location.href = '/menu_UI/" + deskID + "/" + typeID + "/1';</script>");
        }
    }

    /**
     * 查看当前购物车中的所有菜品
     */
    @GetMapping("/checkCart/{deskID}/{typeID}")
    public String checkCart(@PathVariable("deskID") Integer deskID, @PathVariable("typeID") Integer typeID,  ModelMap map)
    {
        //1.获取数据
        List<Cart> carts = cartService.getCarts(deskID); //这张桌子上的所有菜品
        List<FoodType> foodTypes = foodTypeService.getAllFoodType(); //可用菜系
        Desk desk = deskService.getDeskByID(deskID); //当前餐桌
        double totalPrice = CartsTotalPriceUtils.getTotalPrice(carts);

        //2.绑定数据
        map.addAttribute("carts", carts);
        map.addAttribute("foodTypes", foodTypes);
        map.addAttribute("desk", desk);
        map.addAttribute("type_id", typeID);
        map.addAttribute("totalPrice", totalPrice);
        map.addAttribute("flag", true);

        return "clientCart";
    }

    /**
     * 修改购物车中菜品的数量
     */
    @GetMapping("/updateNumber/{number}/{cartID}/{deskID}/{typeID}")
    public String updateNumber(@PathVariable("number") Integer number,
                              @PathVariable("cartID") Integer cartID,
                              @PathVariable("deskID") Integer deskID,
                              @PathVariable("typeID") Integer typeID,
                              ModelMap modelMap)
    {
        //1.获取购物车和餐桌
        Cart cart = cartService.getCartById(cartID); //当前购物记录
        Desk desk = deskService.getDeskByID(deskID); //当前餐桌

        //2.修改num和total
        cart.setNum(number);
        cart.setTotal(cart.getNum() * cart.getFood().getPrice());

        //3.使用更新业务
        cartService.updateCart(cart);

        //4.回显数据
        List<Cart> carts = cartService.getCarts(deskID); //当前餐桌的所有新的购物记录
        List<FoodType> foodTypes = foodTypeService.getAllFoodType(); //可用菜系
        double totalPrice = CartsTotalPriceUtils.getTotalPrice(carts);
        modelMap.addAttribute("carts", carts);
        modelMap.addAttribute("foodTypes", foodTypes);
        modelMap.addAttribute("desk", desk);
        modelMap.addAttribute("type_id", typeID);
        modelMap.addAttribute("totalPrice", totalPrice);
        modelMap.addAttribute("flag", true);

        return "clientCart";
    }

    /**
     * 删除购物车中的一条记录
     */
    @GetMapping("/removeCart/{cartID}/{deskID}/{typeID}")
    public String removeCart(@PathVariable("cartID") Integer cartID,
                             @PathVariable("deskID") Integer deskID,
                             @PathVariable("typeID") Integer typeID,
                             ModelMap modelMap)
    {
        //1.删除记录
        cartService.deleteCartById(cartID);

        //2.回显数据
        List<Cart> carts = cartService.getCarts(deskID); //当前餐桌的所有新的购物记录
        List<FoodType> foodTypes = foodTypeService.getAllFoodType(); //可用菜系
        Desk desk = deskService.getDeskByID(deskID); //当前餐桌
        double totalPrice = CartsTotalPriceUtils.getTotalPrice(carts);
        modelMap.addAttribute("carts", carts);
        modelMap.addAttribute("foodTypes", foodTypes);
        modelMap.addAttribute("desk", desk);
        modelMap.addAttribute("type_id", typeID);
        modelMap.addAttribute("totalPrice", totalPrice);
        modelMap.addAttribute("flag", true);

        return "clientCart";
    }
}
